/*
 * @Author: Snitro
 * @Date: 2021-05-16 11:16:56
 * @LastEditors: Snitro
 * @LastEditTime: 2021-05-25 19:54:52
 * @Description: file content
 */

#include "outputer.h"

int uic_list_size = 21,
    uic_list[][2] = {{0, 16},     {50, 464},   {100, 905},  {150, 1348},
                     {200, 1802}, {250, 2255}, {300, 2706}, {350, 3162},
                     {400, 3619}, {450, 4069}, {500, 4525}, {550, 4977},
                     {600, 5430}, {650, 5887}, {700, 6342}, {750, 6790},
                     {800, 7244}, {850, 7699}, {900, 8153}, {950, 8609},
                     {1000, 9062}};

int uid_list_size = 21,
    uid_list[][2] = {{0, 1},   {5, 6},   {10, 10}, {15, 15}, {20, 20}, {25, 24},
                     {30, 29}, {35, 33}, {40, 38}, {45, 42}, {50, 47}, {55, 50},
                     {60, 54}, {65, 58}, {70, 62}, {75, 67}, {80, 72}, {85, 76},
                     {90, 81}, {95, 85}, {100, 90}};

extern TIM_HandleTypeDef htim2;

void outputer(uint8_t selector) {
    switch (selector) {
        case 0:
            vcom_output(getUicSet());
            break;

        case 1:
            vdif_output(getUidSet());
            break;

        case 2:
            frequency_output(getfidSet());
            break;
    }
}

void frequency_output(int frequency) { AD9833_SetWaveData(frequency, 0); }

void vcom_output(uint32_t vc) {
    int i;
    for (i = 0; i < uic_list_size - 1; i++)
        if (vc >= uic_list[i][0] && vc <= uic_list[i + 1][0]) {
            if (vc == uic_list[i][0])
                __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, uic_list[i][1]);
            else if (vc == uic_list[i + 1][0])
                __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2,
                                      uic_list[i + 1][1]);
            else {
                __HAL_TIM_SET_COMPARE(
                    &htim2, TIM_CHANNEL_2,
                    (int)(uic_list[i][1] +
                          (uic_list[i + 1][1] - uic_list[i][1]) *
                              (vc - uic_list[i][0]) /
                              (uic_list[i + 1][0] - uic_list[i][0])));
            }

            break;
        }
}

void vdif_output(uint32_t vd) {
    int i;
    for (i = 0; i < uid_list_size - 1; i++)
        if (vd >= uid_list[i][0] && vd <= uid_list[i + 1][0]) {
            if (vd == uid_list[i][0])
                X9C104_Set_Step(uid_list[i][1]);
            else if (vd == uid_list[i + 1][0])
                X9C104_Set_Step(uid_list[i + 1][1]);
            else
                X9C104_Set_Step(
                    (int)(uid_list[i][1] +
                          (uid_list[i + 1][1] - uid_list[i][1]) *
                              (vd - uid_list[i][0]) /
                              (uid_list[i + 1][0] - uid_list[i][0])));

            break;
        }
}

void relay_driver(uint8_t PinState) {
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, (GPIO_PinState)(!PinState));
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, (GPIO_PinState)PinState);
}

void leak_valve(uint8_t PinState) {
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, (GPIO_PinState)PinState);
}